﻿/*----------------------------------------------------------------
 * 
 * 版权所有：Copyright (C) 2012 
 * 
 * 文件名：CCompanyController.cs
 * 文件功能描述：
 * 
 * 
 * 作 者：zhangq
 * 邮 箱：zhangqiang@bamabest.com
 * 日 期：2013-02-27 09:19:56
 * 
 * 
 * 修改标识： Author
 * 修改描述： 相关描述
 * 
 * ---------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common.Extensions.OrderBy;
using Common.Extensions.Where;
using DomainModel;
using IManager;
using Webdiyer.WebControls.Mvc;

namespace WebPlatform.Areas.CPP.Controllers
{

    /// <summary>
    /// 
    /// </summary>
    public class CCompanyController : Controller
    {

        public IManagerCompany IMCompany { get; set; }

        public IManagerAlbum IMAlbum { get; set; }


        public IManagerPhoto IMPhoto { get; set; }

        /// <summary>
        /// 产品业务
        /// </summary>
        public IManagerProduct IMProduct { get; set; }

        public IManagerLinks IMLinks { get; set; }

        /// <summary>
        /// 公司首页
        /// </summary>
        public ActionResult Index(int cid = 0)
        {
            var companyId = cid;
            var company = IMCompany.GetCompany(companyId);
            ViewBag.Company = company;
            var w = PredicateExtension.True<ProductInfo>();
            w = w.And(m => m.CompanyId == companyId && m.IsChecked == 1);
            var count = IMProduct.LoadProductCount(w);
            var list = IMProduct.LoadProductAll(w, 1, 12, new OrderByExpression<ProductInfo, DateTime>(u => u.AddDate, true));
            ViewBag.Products = list;

            return View();
        }



        /// <summary>
        /// 产品详情
        /// </summary>
        /// <returns></returns>
        public ActionResult PDetail(int pid = 0)
        {
            var product = IMProduct.GetProduct(pid);
            var companyId = product.CompanyId;
            var company = IMCompany.GetCompany(companyId);
            ViewBag.Company = company;
            return View(product);
        }


        /// <summary>
        /// 企业头
        /// </summary>
        /// <param name="company"></param>
        /// <returns></returns>
        public ActionResult CHeader(CompanyInfo company)
        {
            return PartialView(company);
        }


        /// <summary>
        /// 友情链接
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public ActionResult CLinks(int cid = 0)
        {
            var where = PredicateExtension.True<LinksInfo>();
            where = where.And(u => u.CompanyId == cid && u.Types == "1");

            var linksCount = IMLinks.LoadCount(where);
            var linksList = IMLinks.LoadAll(where, 1, linksCount, new OrderByExpression<LinksInfo, int>(u => u.Id));

            ViewBag.Links = linksList;
            return PartialView();
        }

        /// <summary>
        /// 企业联系方式
        /// </summary>
        /// <param name="company"></param>
        /// <returns></returns>
        public ActionResult CContact(CompanyInfo company)
        {
            return PartialView(company);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public ActionResult CLeft(int cid = 0)
        {
            var companyId = cid;

            var w = PredicateExtension.True<ProductInfo>();
            w = w.And(m => m.CompanyId == companyId && m.IsChecked == 1);
            var count = IMProduct.LoadProductCount(w);

            var list = IMProduct.LoadProductAll(w, 1, count, new OrderByExpression<ProductInfo, DateTime>(u => u.AddDate, true));


            var partitions = list
                                 .GroupBy(u => u.PartitionId)
                                 .Select(m => m.ElementAtOrDefault(0).Partition)
                                 .GroupBy(u => u.ParentId)
                                 .Select(m => new ProductPartitionInfo
                                 {
                                     Name = m.ElementAtOrDefault(0).ProductPartition.Name,
                                     PartitionId = m.ElementAtOrDefault(0).ProductPartition.PartitionId,
                                     Children = m.Select(me => me).ToList()
                                 });
            ViewBag.ProductPartitions = partitions;


            var company = IMCompany.GetCompany(companyId);
            ViewBag.Company = company;


            return PartialView();
        }



        public ActionResult CPDetail(int cid = 0)
        {
            var companyId = cid;
            var company = IMCompany.GetCompany(companyId);
            ViewBag.Company = company;
            return View();
        }


        public ActionResult CPAddress(int cid = 0)
        {
            var companyId = cid;
            var company = IMCompany.GetCompany(companyId);
            ViewBag.Company = company;
            return View();
        }


        public ActionResult CProducts(string partitionId, int cid = 0, int page = 1, int pageSize = 12)
        {
            var companyId = cid;
            var company = IMCompany.GetCompany(companyId);
            ViewBag.Company = company;



            var p = PredicateExtension.True<ProductInfo>();
            p = p.And(u => u.IsChecked == 1 && u.CompanyId == cid);
            if (!string.IsNullOrEmpty(partitionId))
            {
                var partition = IMProduct.GetPartition(partitionId);
                if (partition != null)
                {
                    if (partition.HasChildren == 0)
                    {
                        ViewBag.PartitionName = " > " + partition.ProductPartition.Name + " > " + partition.Name;
                    }
                    else
                    {
                        ViewBag.PartitionName = " > " + partition.Name;
                    }
                }

                p = p.And(u => u.PartitionId.StartsWith(partitionId));
            }
            var count = IMProduct.LoadProductCount(p);
            var list = IMProduct.LoadProductAll(p, page, pageSize, new OrderByExpression<ProductInfo, DateTime>(u => u.AddDate, true));

            PagedList<ProductInfo> plist =
                new PagedList<ProductInfo>(list, page, pageSize, count);


            return View(plist);

        }


        /// <summary>
        /// 宣传视频
        /// </summary>
        /// <returns></returns>
        public ActionResult PT(int cid = 0)
        {
            var companyId = cid;
            var company = IMCompany.GetCompany(companyId);
            ViewBag.Company = company;
            return View();
        }

        public ActionResult CAblums(int cid = 0)
        {
            var companyId = cid;
            var company = IMCompany.GetCompany(companyId);
            ViewBag.Company = company;

            var where = PredicateExtension.True<AlbumInfo>();
            where = where.And(u => u.CompanyId == companyId);
            var count = IMAlbum.LoadCount(where);
            var list = IMAlbum.LoadAll(where, 1, count,
                new OrderByExpression<AlbumInfo, int>(u => u.AlbumId, true));


            return View(list);
        }


        public ActionResult CPhotos(int cid = 0, int aid = 0, int page = 1, int pageSize = 12)
        {

            var companyId = cid;
            var company = IMCompany.GetCompany(companyId);
            ViewBag.Company = company;

            var p = PredicateExtension.True<PhotoInfo>();

            p = p.And(u => u.AlbumId == aid);

            var count = IMPhoto.LoadCount(p);
            var list = IMPhoto.LoadAll(p, page, pageSize,
                new OrderByExpression<PhotoInfo, DateTime>(u => u.UploadDate, true));
            PagedList<PhotoInfo> plist =
                new PagedList<PhotoInfo>(list, page, pageSize, count);

            return View(plist);
        }


        public ActionResult CPhoto(int cid = 0,int pid = 0)
        {
             var companyId = cid;
            var company = IMCompany.GetCompany(companyId);
            ViewBag.Company = company;
            var photo = IMPhoto.GetPhoto(pid);
            return View(photo);
        }
    }
}
